package com.masterclient.app.businessmodule.vipmodule.view;

import android.content.Context;
import android.support.annotation.Nullable;
import android.text.Html;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.eoner.baselibrary.utils.DensityUtil;
import com.eoner.baselibrary.utils.GlideUtil;
import com.eoner.baselibrary.utils.Utils;
import com.masterclient.app.R;
import com.masterclient.app.businessmodule.vipmodule.bean.Order;
import com.masterclient.app.businessmodule.vipmodule.bean.Products;
import com.masterclient.app.businessmodule.vipmodule.bean.ServiceOrder;
import com.masterclient.app.utils.CommonUtil;
import com.masterclient.app.widget.CircleImage;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 订单view
 */
public class OrderView extends LinearLayout {

    @BindView(R.id.name_tv)
    TextView nameTv;
    @BindView(R.id.time_tv)
    TextView timeTv;
    @BindView(R.id.status_tv)
    TextView statusTv;
    @BindView(R.id.no_tv)
    TextView noTv;
    @BindView(R.id.shifu_tv)
    TextView shifuTv;
    @BindView(R.id.container_product)
    LinearLayout containerProduct;
    @BindView(R.id.total_tv)
    TextView totalTv;
    @BindView(R.id.amount_tv)
    TextView amountTv;
    @BindView(R.id.avatar_iv)
    CircleImage avatarIv;

    public OrderView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        setOrientation(LinearLayout.VERTICAL);
        initView();
    }

    private void initView() {
        LayoutInflater.from(getContext()).inflate(R.layout.order_view, this);
        ButterKnife.bind(this);
        Utils.setDin(amountTv, getContext());
    }

    /**
     * 设置订单信息
     *
     * @param order
     */
    public void setOrder(Order order) {
        Order.OrderBean bean;
        if (order instanceof ServiceOrder) {
            bean = new Order.OrderBean();
            bean.setServiceOrder((ServiceOrder) order);
            statusTv.setText(bean.getStatusText());
        }else {
            bean = order.getOrder();
            statusTv.setText(order.getStatusText());
        }
        String create_at = bean.getCreate_at();
        String no = bean.getOrder_no();
        if (order instanceof ServiceOrder) {// 售后订单
            shifuTv.setText("合计:");
            String time = "<font color=\"#333333\">申请时间:</font>　" + create_at;
            String num = "<font color=\"#333333\">退单编号:</font>　" + no;
            timeTv.setText(Html.fromHtml(time));
            noTv.setText(Html.fromHtml(num));
        } else {
            String time = "<font color=\"#333333\">下单时间:</font>　" + create_at;
            String num = "<font color=\"#333333\">订单编号:</font>　" + no;
            timeTv.setText(Html.fromHtml(time));
            noTv.setText(Html.fromHtml(num));
        }
        String avatar = bean.getBuyer_headimage();
        if (null != avatar)
            GlideUtil.displayImage(getContext(), avatar, avatarIv, R.mipmap.user_head_default_icon);
        nameTv.setText(bean.getBuyer_name());
        String productNum = bean.getProduct_num();
        totalTv.setText("共 " + productNum + " 件产品");
        amountTv.setText("¥ " + bean.getSubtotal());
        containerProduct.removeAllViews();
        List<Products> productList = bean.getProducts();
        if(null == productList)
            return;
        for (int i = 0; i < productList.size(); i++) {
            View view = addProductItem(productList.get(i));
            if (i > 0)
                view.setPadding(0, DensityUtil.dip2px(8), 0, 0);
            containerProduct.addView(view);
        }
    }

    /**
     * @return 动态添加产品信息
     */
    private View addProductItem(Products products) {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.product_item, null);
        ImageView imageView = view.findViewById(R.id.product_iv);
        String img = products.getImage();
        if (null != img)
            GlideUtil.displayImage(getContext(), img, imageView, R.drawable.product_default);
        TextView nameTv = view.findViewById(R.id.title_tv);
        nameTv.setText(products.getName());
        TextView priceTv = view.findViewById(R.id.price_tv);
        priceTv.setText("¥ " + products.getPrice());
        TextView numTv = view.findViewById(R.id.num_tv);
        Utils.setDin(priceTv, getContext());
        Utils.setDin(numTv, getContext());
        numTv.setText("X " + products.getBuy_qty());
        TextView specificationsTv = view.findViewById(R.id.specifications_tv);
        specificationsTv.setText(products.getAttribute_desc());
        return view;
    }


    @OnClick(R.id.no_tv)
    public void onViewClicked() {
        String no = noTv.getText().toString().trim();
        if(!TextUtils.isEmpty(no))
            CommonUtil.copy(getContext(),no);
    }
}
